<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

    <bean class="ru.tbcarus.topjava.web.json.JacksonObjectMapper" id="objectMapper" factory-method="getMapper"/>

    <!-- serve static resources (*.html, ...) from src/main/webapp/ -->
    <mvc:default-servlet-handler/>

    <security:global-method-security secured-annotations="enabled" pre-post-annotations="enabled"/>

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper" ref="objectMapper"/>
            </bean>
        </mvc:message-converters>
        <mvc:argument-resolvers>
            <bean class="org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver "/>
        </mvc:argument-resolvers>
    </mvc:annotation-driven>

    <context:component-scan base-package="ru.tbcarus.**.web"/>

    <!--  all resources inside folder src/main/webapp/resources are mapped so they can be referred to inside JSP files -->
    <mvc:resources mapping="/resources/**" location="/resources/"/>

    <!-- use WebJars so Javascript and CSS libs can be declared as Maven dependencies (Bootstrap, jQuery...) -->
    <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

    <!--  Swagger2  https://medium.com/@andreymamontov/swagger-2-configuration-with-spring-xml-3cd643a12425 -->
    <bean class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration"/>
    <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp"/>

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
          p:cacheSeconds="5"
          p:defaultEncoding="UTF-8">
        <property name="basenames" value="file:///#{systemEnvironment[VOTES_ROOT]}/config/messages/app"/>
        <property name="fallbackToSystemLocale" value="false"/>
    </bean>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="ru"/>
    </bean>

    <mvc:interceptors>
        <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="lang"/>
        </bean>
    </mvc:interceptors>
</beans>